// pages/addAdressMap/addAdressMap.js
let QQMapWX = require('qqmap-wx-jssdk.min');
let qqmapsdk;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    myLatitude:0.0,
    myLongitude:0.0,
    addressList:[],
    city:"",
    markers:[],
    points:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    qqmapsdk = new QQMapWX({
      key:'BKKBZ-YQR6F-H2EJG-JCXFA-LEPTF-SVB2J'
    });

    wx.getLocation({
      type:'gcj02',
      success: function(res) {
        console.log(res.latitude,res.longitude)
        that.setData({
          myLatitude: res.latitude,
          myLongitude: res.longitude,
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    //this.getLngLat()
  },

  getLngLat:function(e){
    let that = this;
    that.mapCtx = wx.createMapContext("map");
    that.mapCtx.getCenterLocation({
      success:function(res){
        that.setData({
          markers:[{
            id:0,
            iconPath:"../images/tuding.png",
            longitude:res.longitude,
            latitude:res.latitude,
            width:22,
            height:32
          }]
        })
        that.getPoiList(res.longitude, res.latitude)
      }
    })
  },

  regionchange:function(e){
    var that = this;
    console.log(e)
    if (e.type == 'end'){
      that.getLngLat()
    }
    // e.type == 'end' ? that.getLngLat() : return
  },
  getPoiList:function(longitude,latitude){
    var that = this;
    qqmapsdk.reverseGeocoder({
      location: {
        latitude: latitude,
        longitude: longitude,
      },
      get_poi:1,
      poi_options:'policy=2;radius=3000;page_size=8;page_index=1',
      success:function(res){
        console.log(res);
        var code = res.result.pois[0].ad_info.adcode;
        that.setData({
          addressList:res.result.pois,
          city: res.result.pois[0].ad_info.city
        });
        that.getCarInfo(code)
      },
      fail:function(res){
        console.log(res)
      },
      complete:function(res){
        console.log("map完成")
      }
    })

  },
  getCarInfo: function (code) {
    const that = this;
    if (code != "") {
      wx.request({
        url: getApp().globalData.requestURL + '/api/vehicle/getVehiclesByAgentId',
        data: {
          code: code
        },
        method: 'GET',
        success: function (res) {
          console.log("代理商车辆信息")
          console.log(res)
          var carList = res.data.result;
          if (carList.length == 0 || carList == null) {
            wx.showModal({
              title: '提示',
              content: '抱歉，当前位置没有服务商',
              showCancel: false,
              success(res) { }
            })
          } 

        },
        fail: function (err) {
          console.log(err.errMsg)
        }

      })
    }else{
      wx.showModal({
        title: '提示',
        content: '获取位置信息失败',
        showCancel: false,
        success(res) { 
          wx.navigateBack({
            delta:1
          })
        }
      })
    }
  },
  select_address:function(e){
    console.log(e)
    
    //获取上一个页面
    var info = e.currentTarget.dataset.item
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];
    var location = info.location.lat + "," + info.location.lng;
    console.log(info.ad_info.adcode)
    //刷新上一页面的数据
    prevPage.setData({
      select_address: info.address,
      location: location,
      adcode:info.ad_info.adcode
    })
    if(info){
      wx.navigateBack({
        delta:1
      })
    }
  },
  getString:function(e){
  },
  searchString:function(e){
    console.log("点击搜索按钮")
    var that = this;
    console.log(e.detail.value);
    qqmapsdk.search({
      keyword: e.detail.value,  //搜索关键词
      location: {
        latitude: that.data.myLatitude,
        longitude: that.data.myLongitude
      },  //设置周边搜索中心点
      page_size: 8,
      region: that.data.city,
      success: function (res) { //搜索成功后的回调
        console.log(res)
        that.setData({
          addressList: res.data,
        })
      },
      fail: function (res) {
        console.log(res);
      },
      complete: function (res) {
        console.log(res);
      }
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})